//
//  AppDelegate.m
//  MarianasTrench
//
//  Created by wangjue on 16/3/17.
//  Copyright © 2016年 王爵. All rights reserved.
//

#import "AppDelegate.h"

@interface AppDelegate ()

@property (nonatomic, strong) UITabBarController *tab;
@property (nonatomic, strong) UIImageView *musicImage;
@property (nonatomic, assign) BOOL isPlay;

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [self.window makeKeyAndVisible];
    self.window.backgroundColor = [UIColor whiteColor];
    
    //填入申请的key
    [Bmob registerWithAppKey:@"3fa1f2185375091820de0d9dcbd2e836"];
    
    
    //创建发现页面
    DiscoverViewController *discoverVC = [[DiscoverViewController alloc] init];
    UINavigationController *discoverNav = [[UINavigationController alloc] initWithRootViewController:discoverVC];
    
    UIImage *discoverImage = [UIImage imageNamed:@"iconfont-faxian.png"];
    discoverVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"发现" image:discoverImage tag:1001];
    
    //创建定制听页面
    ListenViewController *listenVC = [[ListenViewController alloc] init];
    UINavigationController *listenNav = [[UINavigationController alloc] initWithRootViewController:listenVC];
    
    UIImage *listenImage = [UIImage imageNamed:@"iconfont-tingyinle"];
    listenVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"定制听" image:listenImage tag:1002];
    
    //创建分类页面
    KindsViewController *kindVC = [[KindsViewController alloc] init];
    UINavigationController *kindNav = [[UINavigationController alloc] initWithRootViewController:kindVC];
    
    UIImage *kindImage = [UIImage imageNamed:@"iconfont-fenlei.png"];
    kindVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"分类" image:kindImage tag:1003];
    
    
    //创建我的页面
    MyselfViewController *myselfVC = [[MyselfViewController alloc] init];
    UINavigationController *myselfNav = [[UINavigationController alloc] initWithRootViewController:myselfVC];
    
    UIImage *myselfImage = [UIImage imageNamed:@"iconfont-wode.png"];
    myselfVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"我的" image:myselfImage tag:1004];
    
    self.tab = [[UITabBarController alloc] init];
    _tab.viewControllers = @[discoverNav, listenNav, kindNav, myselfNav];
    self.window.rootViewController = _tab;
    
    _tab.tabBar.hidden = YES;
    
    [self createSubViews];
    
    return YES;
}


#pragma mark - 创建子视图
- (void)createSubViews {
    /** 自定义tabBar */
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HETGHT - 49, SCREEN_WIDTH, 49)];
    view1.backgroundColor = [UIColor whiteColor];
    [self.window addSubview:view1];
    
    /** 发现 */
    UIButton *discoverButton = [UIButton buttonWithType:UIButtonTypeCustom];
    discoverButton.frame = CGRectMake(SCREEN_WIDTH / 15, 5, SCREEN_WIDTH / 15, SCREEN_WIDTH / 15);
    [discoverButton setImage:IMAGE(@"iconfont-faxian.png") forState:UIControlStateNormal];
    discoverButton.showsTouchWhenHighlighted = YES;
    [view1 addSubview:discoverButton];
    [discoverButton addTarget:self action:@selector(discoverButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *discoverLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, discoverButton.frame.origin.y + discoverButton.frame.size.height + 5, SCREEN_WIDTH / 5, 10)];
    discoverLabel.text = @"发现";
    discoverLabel.textColor = [UIColor redColor];
    discoverLabel.textAlignment = NSTextAlignmentCenter;
    discoverLabel.font = [UIFont systemFontOfSize:10];
    [view1 addSubview:discoverLabel];
    
    /** 视听 */
    UIButton *listenButton = [UIButton buttonWithType:UIButtonTypeCustom];
    listenButton.frame = CGRectMake(SCREEN_WIDTH / 15 * 4, 5, SCREEN_WIDTH / 15, SCREEN_WIDTH / 15);
    [listenButton setImage:IMAGE(@"iconfont-shiting.png") forState:UIControlStateNormal];
    listenButton.showsTouchWhenHighlighted = YES;
    [view1 addSubview:listenButton];
    [listenButton addTarget:self action:@selector(listenButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *listenLabel = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH / 5, listenButton.frame.origin.y + listenButton.frame.size.height + 5, SCREEN_WIDTH / 5, 10)];
    listenLabel.text = @"定制听";
    listenLabel.textColor = [UIColor redColor];
    listenLabel.font = [UIFont systemFontOfSize:10];
    listenLabel.textAlignment = NSTextAlignmentCenter;
    [view1 addSubview:listenLabel];

    
    /** 分类 */
    UIButton *kindButton = [UIButton buttonWithType:UIButtonTypeCustom];
    kindButton.frame = CGRectMake(SCREEN_WIDTH / 15 * 10, 5, SCREEN_WIDTH / 15, SCREEN_WIDTH / 15);
    [kindButton setImage:IMAGE(@"iconfont-111i.png") forState:UIControlStateNormal];
    kindButton.showsTouchWhenHighlighted = YES;
    [view1 addSubview:kindButton];
    [kindButton addTarget:self action:@selector(kindButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *kindLabel = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH / 5 * 3, kindButton.frame.origin.y + kindButton.frame.size.height + 5, SCREEN_WIDTH / 5, 10)];
    kindLabel.text = @"分类";
    kindLabel.textColor = [UIColor redColor];
    kindLabel.font = [UIFont systemFontOfSize:10];
    kindLabel.textAlignment = NSTextAlignmentCenter;
    [view1 addSubview:kindLabel];
    
    UIButton *myselfButton = [UIButton buttonWithType:UIButtonTypeCustom];
    myselfButton.frame = CGRectMake(SCREEN_WIDTH / 15 * 13, 5, SCREEN_WIDTH / 15, SCREEN_WIDTH / 15);
    [myselfButton setImage:IMAGE(@"iconfont-geren.png") forState:UIControlStateNormal];
    myselfButton.showsTouchWhenHighlighted = YES;
    [view1 addSubview:myselfButton];
    [myselfButton addTarget:self action:@selector(myselfButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *myselfLabel = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH / 5 * 4, myselfButton.frame.origin.y + myselfButton.frame.size.height + 5, SCREEN_WIDTH / 5, 10)];
    myselfLabel.text = @"个人";
    myselfLabel.textColor = [UIColor redColor];
    myselfLabel.font = [UIFont systemFontOfSize:10];
    myselfLabel.textAlignment = NSTextAlignmentCenter;
    [view1 addSubview:myselfLabel];

    self.musicImage = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH / 5 * 2 + (SCREEN_WIDTH / 5 - SCREEN_WIDTH / 6) / 2, 49 - SCREEN_WIDTH / 6, SCREEN_WIDTH / 6, SCREEN_WIDTH / 6)];
    self.musicImage.image = IMAGE(@"105820l6wu6t4p9mvjs3tt.jpg");
    _musicImage.layer.cornerRadius = SCREEN_WIDTH / 12;
    _musicImage.layer.masksToBounds = YES;
    [view1 addSubview:_musicImage];
    
    
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
    [_musicImage addGestureRecognizer:longPress];
    
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    
    [center addObserver:self selector:@selector(musicChangeAction:) name:@"musicChange" object:nil];
    
}

#pragma mark - 通知实现方法
- (void)musicChangeAction:(NSNotification *)notification {
    [self.musicImage HANDLEIMAGE(notification.object)];
    
    if ([PlayMusic sharePlayMusic].isPlayState == YES) {
        CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
        // 旋转的起始
        animation.fromValue = [NSNumber numberWithFloat:0];
        // 旋转的结束值
        // π 是180° 2π是360°
        animation.toValue = [NSNumber numberWithFloat:M_PI * 2];
        // 时间
        animation.duration = 10;
        // 重复次数
        // NSIntegerMax 一直在重复
        animation.repeatCount = NSIntegerMax;
        // 是否自动回到原来的位置
        animation.autoreverses = NO;
        // 是否按照结束位置继续旋转
        animation.cumulative = YES;
        [self.musicImage.layer addAnimation:animation forKey:@"标记"];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        [self.musicImage addGestureRecognizer:tap];
        _musicImage.userInteractionEnabled = YES;
        _isPlay = YES;
    }
}

#pragma mark - 自定义tabBar点击方法
- (void)longPressAction:(UILongPressGestureRecognizer *)longPress {
    MusicPlayerViewController *musicVc = [[MusicPlayerViewController alloc] init];
    
    musicVc.playMusicArr = [NSMutableArray arrayWithArray:[PlayMusic sharePlayMusic].musicModelArr];
    musicVc.indexNum = [PlayMusic sharePlayMusic].indexNum;
    
    [_tab presentViewController:musicVc animated:YES completion:nil];
}

- (void)tapAction:(UITapGestureRecognizer *)tap {
    
    if (_isPlay == YES) {
        // 暂停
        [[PlayMusic sharePlayMusic] pause];
        
        CFTimeInterval stopTime = [self.musicImage.layer convertTime:CACurrentMediaTime() fromLayer:nil];
        // 设置动画速度
        self.musicImage.layer.speed = 0;
        // 保存时间的偏移量
        self.musicImage.layer.timeOffset = stopTime;
        
    }else {
        // 继续
        [[PlayMusic sharePlayMusic] play];
        
        // 找到时间偏移量
        CFTimeInterval stopTime = self.musicImage.layer.timeOffset;
        // 速度设置为1
        self.musicImage.layer.speed = 1;
        
        // 把beginTime重置
        self.musicImage.layer.beginTime = 0;
        self.musicImage.layer.timeOffset = 0;
        // 设置开始的时间
        //        self.imgView.layer.beginTime = stopTime;
        self.musicImage.layer.beginTime = [self.musicImage.layer convertTime:CACurrentMediaTime() fromLayer:nil] - stopTime;
    }
    // 状态反转
        _isPlay = !_isPlay;

}

- (void)discoverButtonAction:(UIButton *)button {
    _tab.selectedIndex = 0;
}

- (void)listenButtonAction:(UIButton *)button {
    _tab.selectedIndex = 1;
}

- (void)kindButtonAction:(UIButton *)button {
    _tab.selectedIndex = 2;
}

- (void)myselfButtonAction:(UIButton *)button {
    _tab.selectedIndex = 3;
}



- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    // Saves changes in the application's managed object context before the application terminates.
    [[CoreDataManager shareManager] saveContext];
}



@end
